<?php

class ProviderController extends Controller{


    /**
     * Creates a new model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     */
    public function actionCreate($blockId)
    {
        $block = $this->loadBlock($blockId);

        $this->accessDeniedIf(!checkAccess('ownBlock', array('block'=>$block)));


        $provider = new Provider();

        $provider->user_id = user()->id;

        if(isset($_POST['Provider']))
        {
            $provider->attributes=$_POST['Provider'];

            $provider->user = $block->user;

            if($provider->save()){
                setFlash('success', 'Szolgáltató létrehozva.');
                $this->redirect($this->linkToBlockUpdate($block));
            }
        }



        $this->render('create',array(
                'model'=>$provider,
                'block'=>$block
        ));
    }

    /**
     * Updates a particular model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param integer $id the ID of the model to be updated
     * @param integer $blockId the ID of the model to be updated
     */
    public function actionUpdate($id, $blockId)
    {
        $provider=$this->loadModel($id);

        $block = $this->loadBlock($blockId);

        // permission check
        $this->accessDeniedIf(!checkAccess('ownBlock', array('block'=>$block)));

        if(isset($_POST['Provider']))
        {
            $provider->attributes=$_POST['Provider'];

            $provider->user = $block->user;


            if($provider->save()){
                setFlash('success', 'Szolgáltató frissítve.');
                $this->redirect($this->linkToBlockUpdate($block));
            }
        }


        $this->render('update',array(
                'model'=>$provider,
                'block'=>$block
        ));
    }

    /**
     * Deletes a particular model.
     * If deletion is successful, the browser will be redirected to the 'admin' page.
     * @param integer $id the ID of the model to be deleted
     * @param integer $blockId ID of block
     */
    public function actionDelete($id, $blockId)
    {

        $provider=$this->loadModel($id);
        $block=$this->loadBlock($blockId);

        // permission check
        $this->accessDeniedIf(!checkAccess('ownBlock', array('block'=>$block)));

        $provider->delete();


        if(!isset($_GET['ajax']))
            $this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : $this->linkToBlockUpdate($block));
    }

    protected function linkToBlockUpdate($block){

        return $this->createUrl('block/update', array('id'=>$block->id)) . '#providers';

    }


    /**
     * Returns the data model based on the primary key given in the GET variable.
     * If the data model is not found, an HTTP exception will be raised.
     * @param integer the ID of the model to be loaded
     */
    protected function loadModel($id)
    {
        $model=Provider::model()->findByPk($id);
        if($model===null)
            throw new CHttpException(404,'The requested page does not exist.');
        return $model;
    }

    protected function loadBlock($id)
    {
        $model=Block::model()->findByPk($id);
        if($model===null)
            throw new CHttpException(404,'The requested page does not exist.');
        return $model;
    }

    protected function loadUser($id)
    {
        $model=User::model()->findByPk($id);
        if($model===null)
            throw new CHttpException(404,'The requested page does not exist.');
        return $model;
    }

}